package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.unitedvision.core.domain.Goods;

@Entity
@Table (name = "goods")
public class GoodsEntity {
	private int id;
	private GoodsTypeEntity type;
	private String name;
	private String description;
	private List<InventoryEntity> inventories;
	
	public GoodsEntity() {
		super();
	}

	public GoodsEntity(Goods goods) {
		super();
		this.id = goods.getId();
		this.type = GoodsTypeEntity.create(goods.getType());
		this.name = goods.getName();
		this.description = goods.getDescription();
	}

	public GoodsEntity(GoodsTypeEntity type, String name, String description) {
		super();
		this.type = type;
		this.name = name;
		this.description = description;
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne (fetch = FetchType.EAGER)
	@JoinColumn (name = "type")
	public GoodsTypeEntity getType() {
		return type;
	}

	public void setType(GoodsTypeEntity type) {
		this.type = type;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column (name = "description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany (targetEntity = InventoryEntity.class, mappedBy = "goods", fetch = FetchType.LAZY)
	public List<InventoryEntity> getInventories() {
		return inventories;
	}

	public void setInventories(List<InventoryEntity> inventories) {
		this.inventories = inventories;
	}

	public Goods toGoods() {
		return new Goods(id, type.toGoodsType(), name, description);
	}
	
	public static GoodsEntity create(GoodsTypeEntity type, String name, String description) {
		return new GoodsEntity(type, name, description);
	}
	
	public static GoodsEntity create(Goods goods) {
		return new GoodsEntity(goods);
	}

	@Override
	public String toString() {
		return "GoodsEntity [id=" + id + ", type=" + type + ", name=" + name
				+ ", description=" + description + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GoodsEntity other = (GoodsEntity) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}
}
